<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="600px"
      @closed="resetForm('form')"
      :close-on-click-modal="false"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="名称" prop="name">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-radio-group v-model="form.isShow">
              <el-radio label="0">显示</el-radio>
              <el-radio label="1">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="上传" prop="fileId" v-show="title == '新增'">
            <UploadFile
              v-if="dialogVisible"
              :fileType="1"
              @e-fileData="saveFile"
            />
          </el-form-item>
          <el-form-item label="排序" prop="orderBy">
            <el-input-number
              v-model="form.orderBy"
              :min="1"
              :max="1000"
              size="medium"
              label="排序"
            ></el-input-number>
          </el-form-item>
          <!-- <el-form-item label="备注" prop="des">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.des"
            >
            </el-input>
          </el-form-item> -->
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { sysBannerAdd, sysBannerUpdate } from '@/api/admin/system/sysBanner.js'
import UploadFile from '@/components/uploadFile/index.vue'
import Bus from '@/utils/bus.js'
export default {
  components: {
    UploadFile,
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      form: {
        orderBy: 10,
        isShow: '0',
        fileId: '',
        des: '',
        name: '',
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        fileId: [{ required: true, message: '请上传图片', trigger: 'blur' }],
      },
    }
  },
  mounted() {
    Bus.$on('showDialogBannerAdd', this.init)
  },
  beforeDestroy() {
    Bus.$off('showDialogBannerAdd')
  },
  methods: {
    saveFile(v) {
      this.form.fileId = v[0].id || ''
    },
    //显示弹窗
    init(row) {
      if (row) {
        this.form = { ...this.form, ...row }
        this.$set(this.form, 'id', row.id)
      } else {
        this.form = this.$options.data().form
        this.$delete(this.form, 'id')
      }
      this.title = row ? '编辑' : '新增'
      this.dialogVisible = true
    },
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.title == '新增' ? this.add() : this.edit()
        }
      })
    },
    //新增
    add() {
      sysBannerAdd(this.form).then((res) => {
        this.done()
      })
    },
    //编辑
    edit() {
      sysBannerUpdate(this.form).then((res) => {
        this.done()
      })
    },
    done() {
      this.dialogVisible = false
      Bus.$emit('refreshBannerList')
      this.$message({
        message: '操作成功',
        type: 'success',
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
    },
  },
}
</script>